import random

WIDTH = 300
HEIGHT = 300

BACK_COLOR = (12, 200, 120)

class MyRect:
    def __init__(self, x, y, w, h, clr):
        self.x=x
        self.y=y
        self.w=w
        self.h=h
        self.clr=clr
        self.speed=2
    def SetSpeed(self, speed):
        self.speed=speed

    def Update(self):
#        global x
 #       global s
        self.x += self.speed
        if self.x - self.w // self.speed > WIDTH:
            self.speed=-2
        elif self.x - self.w//self.speed < WIDTH/5 and self.speed==-2:
            self.speed=2

    def Draw(self):
        rc=Rect((self.x-self.w//2,self.y-self.h//2),(self.w,self.h))
        screen.draw.filled_rect(rc,self.clr)

#rc = MyRect(0, 100, 100, 100, (0, 200, 0))

clrLst = [
    (44, 100, 134),
    (75, 26, 9),
    (13, 46, 17),
    (4, 59, 45),
    (32, 78, 35),
    (142, 35, 7),
    (252, 5, 6),
]
rcLst = []
for i in range(10):
    w = random.randint(50, 100)
    clrIdx = random.randint(0, 6)
    print(clrIdx)
    clr = clrLst[clrIdx]
    rc = MyRect(0, random.randint(50, 200), w, w, clr)
    rc.SetSpeed(random.randint(1, 10))
    rcLst.append(rc)

def on_key_down(key):
    if key == keys.ESCAPE:
        quit()

def on_mouse_down(pos):
    print("mouse_down", pos)

def on_mouse_up(pos):
    print("mouse_up", pos)

def update():
    for rc in rcLst:
        rc.Update()

def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    for rc in rcLst:
        rc.Draw()